package com.findyou.headline.service.impl;

import com.findyou.headline.dao.NewsHeadlineDao;
import com.findyou.headline.dao.impl.NewsHeadlineDaoImpl;
import com.findyou.headline.pojo.NewsHeadline;
import com.findyou.headline.pojo.vo.HeadlineDetailVo;
import com.findyou.headline.pojo.vo.HeadlinePageVo;
import com.findyou.headline.pojo.vo.HeadlineQueryVo;
import com.findyou.headline.service.NewsHeadlineService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsHeadlineServiceImpl implements NewsHeadlineService {
    private NewsHeadlineDao headlineDao = new NewsHeadlineDaoImpl();
/* pageInfo 可不轻松啊
    要求包含下面这些
    pageData: 数组 [
    {
        "hid"
        "title"
                ......
    }
        ]
    pageNum:1
    pageSize:1
    totalPage:1
    totalSize:1
*/
    @Override
    public Map findPage(HeadlineQueryVo headlineQueryVo) {
        int pageNum = headlineQueryVo.getPageNum();
        int pageSize = headlineQueryVo.getPageSize();
        List<HeadlinePageVo>pageData = headlineDao.findPageList(headlineQueryVo);
        int totalSize = headlineDao.findPageCount(headlineQueryVo);
        int totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
        Map pageInfo = new HashMap();
        pageInfo.put("pageNum", pageNum);
        pageInfo.put("pageSize", pageSize);
        pageInfo.put("totalSize", totalSize);
        pageInfo.put("totalPage", totalPage);
        pageInfo.put("pageData", pageData);
        return pageInfo;
    }

    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        //修改该头条的浏览量 + 1
        int rows = headlineDao.incrPageViews(hid);
        //查询头条的详情
        return headlineDao.findHeadlineDetail(hid);
    }

    @Override
    public void addNewsHeadline(NewsHeadline newsHeadline) {
        headlineDao.addNewsHeadline(newsHeadline);
    }

    @Override
    public NewsHeadline findByHid(int hid) {
        return headlineDao.findByHid(hid);
    }

    @Override
    public void update(NewsHeadline newsHeadline) {
        headlineDao.update(newsHeadline);
    }

    @Override
    public void removeByHid(int hid) {
        headlineDao.removeByHid(hid);
    }
}
